using System;
using Csla;
using System.Data;
using Csla.Data;
using ENT.GP.Datos.MysqlHelper;

namespace ENT.GP.Core
{
    /// <summary>
    /// Estereotipo: EditableRoot
    /// Reponsabilidad: 
    /// Autor: Luis
    /// Fecha: 9/22/2009 12:11:01 AM
    /// </summary>
    [Serializable]
    public class Catalogo : BusinessBase<Catalogo>
    {
        #region Business Methods

        #region < CLAVE PRIMARIA >

        // Identificador
        private static PropertyInfo<int> IdProperty = RegisterProperty<int>(o => o.Id);
        /// <summary>
        /// Identificador de Catalogo
        /// </summary>
        public int Id
        {
            get { return GetProperty(IdProperty); }
        }

        #endregion

        #region < PROPIEDADES >

        private static PropertyInfo<int?> IdCatalogoPadreProperty = RegisterProperty<int?>(o => o.IdCatalogoPadre, "Name");
        public int? IdCatalogoPadre
        {
            get { return GetProperty(IdCatalogoPadreProperty); }
            set { SetProperty(IdCatalogoPadreProperty, value); }
        }

        private static PropertyInfo<string> CodigoProperty = RegisterProperty<string>(o => o.Codigo, "Name");
        public string Codigo
        {
            get { return GetProperty(CodigoProperty); }
            set { SetProperty(CodigoProperty, value); }
        }

        private static PropertyInfo<string> NombreProperty = RegisterProperty<string>(o => o.Nombre, "Name");
        public string Nombre
        {
            get { return GetProperty(NombreProperty); }
            set { SetProperty(NombreProperty, value); }
        }

        private static PropertyInfo<string> DescripcionProperty = RegisterProperty<string>(o => o.Descripcion, "Name");
        public string Descripcion
        {
            get { return GetProperty(DescripcionProperty); }
            set { SetProperty(DescripcionProperty, value); }
        }

        private static PropertyInfo<int> HabitantesRuralProperty = RegisterProperty<int>(o => o.HabitantesRural, "Name");
        public int HabitantesRural
        {
            get { return GetProperty(HabitantesRuralProperty); }
            set { SetProperty(HabitantesRuralProperty, value); }
        }

        private static PropertyInfo<int> HabitantesUrbanoProperty = RegisterProperty<int>(o => o.HabitantesUrbano, "Name");
        public int HabitantesUrbano
        {
            get { return GetProperty(HabitantesUrbanoProperty); }
            set { SetProperty(HabitantesUrbanoProperty, value); }
        }

        //Nuevo Id Empresa
        private static PropertyInfo<int> IDEmpresaProperty = RegisterProperty<int>(o => o.IDEmpresa, "Name");
        public int IDEmpresa
        {
            get { return GetProperty(IDEmpresaProperty); }
            set { SetProperty(IDEmpresaProperty, value); }
        }


        #endregion              

        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(RuleMethod, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("Name", "Role");
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowEdit(typeof(Catalogo), "Role");
        }

        #endregion

        #region Factory Methods

        public static Catalogo New()
        {
            return DataPortal.Create<Catalogo>();
        }

        public static Catalogo Get(int id)
        {
            return DataPortal.Fetch<Catalogo>(
              new SingleCriteria<Catalogo, int>(id));
        }

        public static void Delete(int id)
        {
            DataPortal.Delete(new SingleCriteria<Catalogo, int>(id));
        }

        private Catalogo()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            
                base.DataPortal_Create();
            
        }

        private void DataPortal_Fetch(SingleCriteria<Catalogo, int> criteria)
        {
            DataSet dsDatos = new DataSet();
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[1];
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("IDC", criteria.Value);
            dsDatos = objhm.EjecutarStoreProcedure(parametros, "Obtener_Catalogo");
            if (dsDatos.Tables[0].Rows.Count > 0)
            {
                LoadProperty(IdProperty, Convert.ToInt32(dsDatos.Tables[0].Rows[0][0].ToString()));
                LoadProperty(CodigoProperty, dsDatos.Tables[0].Rows[0][1].ToString());
                LoadProperty(NombreProperty, dsDatos.Tables[0].Rows[0][2].ToString());
                LoadProperty(DescripcionProperty, dsDatos.Tables[0].Rows[0][3].ToString());
                LoadProperty(IdCatalogoPadreProperty, dsDatos.Tables[0].Rows[0][4].ToString());
                LoadProperty(HabitantesUrbanoProperty, Convert.ToInt32(dsDatos.Tables[0].Rows[0][5].ToString()));
                LoadProperty(HabitantesRuralProperty, Convert.ToInt32(dsDatos.Tables[0].Rows[0][6].ToString()));
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[7];
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("CC", Codigo );
            parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("VI", Nombre );
            parametros[2] = new MySql.Data.MySqlClient.MySqlParameter("DE", Descripcion );
            parametros[3] = new MySql.Data.MySqlClient.MySqlParameter("IDCP", IdCatalogoPadre);
            parametros[4] = new MySql.Data.MySqlClient.MySqlParameter("HU", HabitantesUrbano);
            parametros[5] = new MySql.Data.MySqlClient.MySqlParameter("HR", HabitantesRural);
            parametros[6] = new MySql.Data.MySqlClient.MySqlParameter("IDEMP1", IDEmpresa);
            //
            objhm.EjecutarStoreProcedure("Insertar_Catalogo", parametros);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[7];
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("CC", Codigo);
            parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("VI", Nombre);
            parametros[2] = new MySql.Data.MySqlClient.MySqlParameter("DE", Descripcion);
            parametros[3] = new MySql.Data.MySqlClient.MySqlParameter("IDCP", IdCatalogoPadre);
            parametros[4] = new MySql.Data.MySqlClient.MySqlParameter("HU", HabitantesUrbano);
            parametros[5] = new MySql.Data.MySqlClient.MySqlParameter("HR", HabitantesRural);
            parametros[6] = new MySql.Data.MySqlClient.MySqlParameter("IDC", Id );
            objhm.EjecutarStoreProcedure("Actualizar_Catalogo", parametros);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            
                DataPortal_Delete(new SingleCriteria<Catalogo, int>(ReadProperty(IdProperty)));
            
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(SingleCriteria<Catalogo, int> criteria)
        {
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[1];
            
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("IDC", Id);
            objhm.EjecutarStoreProcedure("Eliminar_Catalogo", parametros);
       }

        #endregion
    }
}